package com.wanxi.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.wanxi.model.Permission;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.util.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:Doteon
 * @createDate:2023/3/7
 * @version: 1.0
 */
@Controller
@CrossOrigin(origins = "*")
@Slf4j
public class SendMessageController {
    private static String TOPIC_EXCHANGE="wx.fuzhuang.topic";
    @Resource
    private  RabbitTemplate rabbitTemplate;
    @Autowired
    UserService userService;
    public void addUserMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"增加了一条"+table+"数据";
        permission(message,userName);
    }
    public void deleteUserMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"删除了一条"+table+"数据";
        permission(message,userName);
    }

    public void findAllUserMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"查询了全部"+table+"数据";
        permission(message,userName);
    }

    public void findByIdUserMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"查询了一条"+table+"数据";
        permission(message,userName);
    }
    public void updateUserMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了一条"+table+"数据";
        permission(message,userName);
    }
    public void updatePasswordUserMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了"+table+"密码";
        permission(message,userName);
    }
    public void updateIsRecommendMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了"+table+"推荐";
        permission(message,userName);
    }
    public void updateEnableMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了"+table+"状态";
        permission(message,userName);
    }
    public void updateIsShowMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了"+table+"显示";
        permission(message,userName);
    }
    public void updateWhereShowMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了"+table+"哪里显示";
        permission(message,userName);
    }
    public void updateMessages(String table){
        String userName=SecurityContextHolder.getContext().getAuthentication().getName();
        String message = Tool.time()+"  "+userName+"修改了"+table+"数据";
        permission(message,userName);
    }
    public void loginUserMessages(UserModel model){
        String userName = model.getUsername();
        String message = Tool.time()+"  "+userName+"登录后台";
        permission(message,userName);
    }


    private void permission(String message,String userName){
        UserModel userModel=userService.getUserByName(userName);
        List<Permission> permissionList=userService.getPermissionsByUserId(userModel.getId());
        String permission =permissionList.get(0).getName();
        if ("allPermission".equals(permission)){
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE,"wx.fuzhuang.all", message);
        }else if ("subPermission".equals(permission)){
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE,"wx.fuzhuang.admin.all", message);
        }else {
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE,"wx.fuzhuang.user.all", message);
        }
    }

}
